# GET /meiduo_admin/goods/simple/
#获取SKU管理里面的新增商品找中,SPU名称
from rest_framework.generics import ListAPIView, GenericAPIView
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response

from goods.models import SPU, SPUSpecification
from meiduo_admin.serializers.spus import SPUSimpleSerializer, SPUSpecSerializer


class SPUSimpleView(ListAPIView):
    permission_classes = [IsAdminUser]
    #指定视图使用的查询集
    queryset = SPU.objects.all()
    #指定视图使用的序列化器类
    serializer_class = SPUSimpleSerializer
    # 关闭分页
    pagination_class = None


#GET meiduo_admin/goods/(?P<pk>\d+)/specs/
# class SPUSpecView(GenericAPIView):
#     permission_classes = [IsAdminUser]
#     def get(self,request,pk):
#         '''
#         获取spu规格选项数据
#         1.根据pk获取spu specs数据
#
#
#         :param request:
#         :param pk:
#         :return:
#         '''
#         #1.根据pk获取spu specs数据
#         specs=SPUSpecification.objects.filter(spu_id=pk)
#         #2.将spu数据序列化并返回
#         serializer=SPUSpecSerializer(specs,many=True)
#         return Response(serializer.data)
class SPUSpecView(ListAPIView):
    permission_classes = [IsAdminUser]
    #指定序列化器类
    serializer_class = SPUSpecSerializer
    def get_queryset(self):
        '''返回视图所使用的查询集'''
        #获取pk
        pk=self.kwargs['pk']
        return SPUSpecification.objects.filter(spu_id=pk)
    #关闭分页
    pagination_class = None

